<?php
final class DeviceModel {
	private $mysql;
	private $tableName = 'Device';

	public $idRowName = 'Device_ID';
	public $imeiRowName = 'Device_IMEI';
	public $phoneNumberRowName = 'Device_PhoneNumber';
	public $simSerialNumberRowName = 'Device_SimSerialNumber';
	public $imsiRowName = 'Device_IMSI';
	public $manufacturerRowName = 'Device_Manufacturer';
	public $modelRowName = 'Device_Model';
	public $osRowName = 'Device_OS';
	public $versionRowName = 'Device_Version';
	public $remarkRowName = 'Device_Remark';


	public $CountRowName = 'Device_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

	public function GetDeviceCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}


	public function CheckDeviceItemExistByIMEI ($IMEI) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName),  $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($IMEI)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function CheckDeviceItemExistByID ($ID) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName),  $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($ID)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetDeviceItems ($offset, $count) {
		$ref = array ();
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`,  `%s`,  `%s`,  `%s`, `%s`,  `%s`,  `%s`,  `%s`,  `%s` FROM `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelDeviceItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}


	public function AddDeviceItem ($IMEI, $PhoneNumber, $SimSerialNumber, $IMSI, $Manufacturer, $Model, $OS, $Version, $Remark = '') {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($IMEI), $this->mysql->escape_string ($PhoneNumber), $this->mysql->escape_string ($SimSerialNumber), $this->mysql->escape_string ($IMSI), $this->mysql->escape_string ($Manufacturer), $this->mysql->escape_string ($Model), $this->mysql->escape_string ($OS), $this->mysql->escape_string ($Version), $this->mysql->escape_string ($Remark)));
		$sqlresult->free_result ();
		return $this->mysql->insert_id ();
	}

}
?>
